home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <dos.h>
- #include <sys/farptr.h>
- #include <go32.h>
-
- #define DOSS _go32_info_block.selector_for_linear_memory
-
- #include "aspi.h"
- #include "scsidefs.h"
- #include "tape.h"
-
- #define cstat(x) if (stat < 0) do_cstat(x);
-
- static int dat, stat;
- static unsigned char cdb[6];
- static unsigned long block_size = 20 * 512;
-
- static void check_sense(void)
- {
- if ((aspi_sense[2] & 0x0f) != KEY_NOSENSE)
- {
- switch (aspi_sense[2] & 0x0f)
- {
- case KEY_NOSENSE: /* No Sense */
- case KEY_RECERROR: /* Recovered Error */
- return;
- case KEY_NOTREADY: /* Not Ready */
- printf("Drive not ready yet.\n");
- exit(0);
- case KEY_DATAPROT: /* Data Protect */
- printf("Tape is write-protected.\n");
- exit(0);
- case KEY_BLANKCHK: /* Blank Check */
- printf("Tape is blank. Additional sense is %d\n", aspi_sense[12]);
- exit(0);
- case KEY_MEDIUMERR: /* Medium Error */
- case KEY_HARDERROR: /* Hardware Error */
- case KEY_ILLGLREQ: /* Illegal Request */
- case KEY_UNITATT: /* Unit Attention */
- case KEY_VENDSPEC: /* Vendor Specific */
- case KEY_COPYABORT: /* Copy Abort */
- case KEY_ABORT: /* Abort */
- case KEY_EQUAL: /* Equal (Search) */
- case KEY_VOLOVRFLW: /* Volume Overflow */
- case KEY_MISCOMP: /* Miscompare (Search) */
- case KEY_RESERVED: /* Reserved */
- printf("Tape error!\n");
- exit(1);
- default:
- printf("Unknown sense error %d (0x%x)\n",
- aspi_sense[2] & 0x0f, aspi_sense[2] & 0x0f);
- exit(1);
- }
- }
- if (aspi_sense[2] & SENSE_ILI)
- {
- printf("Block size is not correct.\n");
- exit(1);
- }
- }
-
- static void do_cstat(char *x)
- {
- printf("Error during %s\n", x);
- check_sense();
- exit(1);
- }
-
- void tape_open(void)
- {
- int nh = aspi_init();
- for (dat=0; dat<64*nh; dat++)
- {
- stat = aspi_device_type(dat);
- if (stat == -1)
- continue;
- if (stat == DTYPE_SEQD)
- /* if (stat == DTYPE_CROM) */
- {
- printf("DAT is scsi(%d,%d,%d)\n",
- ASPI_ID2HOSTAD(dat),
- ASPI_ID2TARGET(dat),
- ASPI_ID2LUN(dat));
- tape_set_blocksize(block_size);
- return;
- }
- }
- printf("No tape drives detected\n");
- exit(0);
- }
-
- void tape_rewind(int wait)
- {
- memset(cdb, 0, 6);
- cdb[0] = 0x01;
- cdb[1] = wait ? 0x00 : 0x01;
- fflush(stdout);
- stat = aspi_exec(dat, 0, 0, ASPI_RW_NODATA, cdb, 6);
- cstat("rewind");
- }
-
- void tape_set_blocksize(unsigned long new_block_size)
-
- {
- unsigned char buffer[12];
- block_size = new_block_size;
- memset(cdb, 0, 6);
- cdb[0] = 0x15;
- cdb[4] = 0x0c;
- memset(buffer, 0, sizeof(buffer));
- buffer[2] = 0x10;
- buffer[3] = 8;
- buffer[9] = (block_size) >> 16;
- buffer[10] = (block_size) >> 8;
- buffer[11] = (block_size);
- stat = aspi_exec(dat, buffer, 12, ASPI_RW_WRITE, cdb, 6);
- cstat("mode select");
- }
-
- int tape_read(void *buffer) /* 0=OK, 1=LEOT */
- {
- unsigned char *sense;
- memset(cdb, 0, 6);
- cdb[0] = 0x08;
- cdb[1] = 0x01;
- cdb[4] = 0x01;
- stat = aspi_exec(dat, buffer, block_size, ASPI_RW_READ, cdb, 6);
- cstat("read from tape");
-
- check_sense();
- if (aspi_sense[2] & SENSE_FILEMRK)
- return 1;
- return 0;
- }
-
- void tape_write(void *buffer)
- {
- memset(cdb, 0, 6);
- cdb[0] = 0x0a;
- cdb[1] = 0x01;
- cdb[4] = 0x01;
- stat = aspi_exec(dat, buffer, block_size, ASPI_RW_WRITE, cdb, 6);
- cstat("write to tape");
-
- check_sense();
- }
-
- void tape_write_filemark(int count, int isshort)
- {
- memset(cdb, 0, 6);
- cdb[0] = 0x10;
- cdb[4] = count;
- cdb[5] = isshort ? 0x80 : 0x00;
- stat = aspi_exec(dat, 0, 0, ASPI_RW_NODATA, cdb, 6);
- cstat("file mark write");
- }
-
- void tape_close(void)
- {
- tape_rewind(0);
- aspi_close();
- }
-
- char *tape_fmtnum(unsigned long l)
- {
- /* 0,000,000,000 */
- static char rv[14];
- char buf[14];
- int i, j;
- sprintf(buf, "%010lu", l);
- for (i=0, j=0; i<13; i++, j++)
- {
- if ((j % 3) == 1)
- rv[i++] = ',';
- rv[i] = buf[j];
- }
- rv[i++] = 0;
- for (i=0; rv[i]; i++)
- {
- if (rv[i] == '0' || rv[i] == ',')
- rv[i] = ' ';
- else
- break;
- }
- return rv;
- }
-